<?php
namespace Zxf5115\Laravel\Phrase\Controllers\Platform;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Zxf5115\Laravel\Phrase\Logics\PhraseLogic;
use Zxf5115\Laravel\Kernel\Traits\ValidatorTrait;
use Zxf5115\Laravel\Kernel\Controllers\Platform\PlatformController;

/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2025-10-17
 *
 * 短语控制器类
 */
class PhraseController extends PlatformController
{
  use ValidatorTrait;

  /**
   * 关键字查询配置
   */
  protected array $allowKeywordParams = [
    'receive' => 'keyword',
    'match' => [
      'name',
      'title',
      'content',
    ]
  ];

  /**
   * 操作短语信息
   *
   * @param Request $request 用户请求参数
   * @return JsonResponse 响应数据
   */
  public function handle(Request $request, PhraseLogic $logic) : JsonResponse
  {
    try
    {
      $messages = [
        'title.required' => '请您输入短语标题',
        'name.required' => '请您输入短语名称',
        'content.required' => '请您输入短语答案',
      ];

      $rules = [
        'title' => 'required',
        'name' => 'required',
        'content' => 'required',
      ];

      // 验证用户数据内容是否正确
      $validation = $this->validation($request, $messages, $rules);

      if(!$validation['status'])
      {
        return error($validation['message']);
      }

      // 资讯短语数据处理
      $logic->handle($request, $this->modelClass);

      return success(__('kernel.handle_success'));
    }
    catch(\Exception $e)
    {
      // 记录异常信息
      record($e);

      return error(__('kernel.handle_failure'));
    }
  }
}
